﻿using System.Windows;
using System.Windows.Controls;
using WPNextTools.NBase;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Linq;

namespace WPNextTools.NControls
{
    public class NPageSpliter : NControl
    {
        #region Fields
        internal StackPanel _esContainer;
        internal int _totalCountCache;
        internal int _selectedIndexCache;
        #endregion

        #region Constructor
        public NPageSpliter()
        {
            base.DefaultStyleKey = typeof(NPageSpliter);
        }
        #endregion

        #region DependencyProperties

        public int TotalCount
        {
            get { return (int)GetValue(TotalCountProperty); }
            set { SetValue(TotalCountProperty, value); }
        }

        public static readonly DependencyProperty TotalCountProperty =
            DependencyProperty.Register("TotalCount", typeof(int), typeof(NPageSpliter), new PropertyMetadata(OnTotalCountChanged));

        private static void OnTotalCountChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as NPageSpliter).OnTotalCountChanged(e);
        }

        private void OnTotalCountChanged(DependencyPropertyChangedEventArgs e)
        {
            _totalCountCache = (int)e.NewValue;
        }

        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(NPageSpliter), new PropertyMetadata(OnSelectedIndexChanged));

        private static void OnSelectedIndexChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as NPageSpliter).OnSelectedIndexChanged(e);
        }

        private void OnSelectedIndexChanged(DependencyPropertyChangedEventArgs e)
        {
            
            if (e.OldValue != e.NewValue)
            {
                int oldIndex = (int)e.OldValue;
                int newIndex = (int)e.NewValue;
                _selectedIndexCache = newIndex;
                UIElementCollection elements = _esContainer.Children;
                Ellipse oldEl = (Ellipse)elements.ElementAt(oldIndex);
                if (oldEl != null)
                {
                    oldEl.Fill = TranslateColor("FFF2EFED");
                }
                Ellipse newEl = (Ellipse)elements.ElementAt(newIndex);
                if (newEl != null)
                {
                    newEl.Fill = TranslateColor("FFFBC038");
                }
            }
        }

        public double Radius { get; set; }
        #endregion

        #region ApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _esContainer = base.GetTemplateChild("ES") as StackPanel;

            for (int i = 0; i < _totalCountCache; i++)
            {
                Ellipse e = new Ellipse();
                e.Margin = new Thickness(5, 0, 5, 0);
                e.Width = Radius;
                e.Height = Radius;
                if (i == _selectedIndexCache)
                {
                    e.Fill = TranslateColor("FFFBC038");
                }
                else
                {
                    e.Fill = TranslateColor("FFF2EFED");
                }
                _esContainer.Children.Add(e);
            }
        }
        #endregion

        #region Method
        private SolidColorBrush TranslateColor(string hexColorStr)
        {
            byte a = System.Convert.ToByte(hexColorStr.Substring(0, 2), 16);
            byte r = System.Convert.ToByte(hexColorStr.Substring(2, 2), 16);
            byte g = System.Convert.ToByte(hexColorStr.Substring(4, 2), 16);
            byte b = System.Convert.ToByte(hexColorStr.Substring(6, 2), 16);
            Color s = Color.FromArgb(a, r, g, b);
            return new SolidColorBrush(s);
        }
        #endregion

    }
}
